import { createStore } from "vuex";
import { contentPath } from "@/constant/system.js"

export default createStore({
  state: {
    // 登录的用户信息
    userInfo: undefined,
    // 侧边栏是否收起
    isCollapse: false,
    // 面包屑导航路径
    crumbsName: "",
    // 打开的菜单tab数组
    tabList: [],
    // 选中tab
    selectedTab: {},
    //权限列表
    menuList: [],
    // token
    token: undefined,
    // 页面是否放大 true=放大
    amplify:false,
    
  },
  getters: {

  },
  mutations: {
    setAmplify(state,val){
      state.amplify = val
    },
    // 登录的用户信息
    setUserInfo: (state, val) => {
      state.userInfo = val;
    },
    // 菜单伸缩状态
    setCollapse: (state, val) => {
      state.isCollapse = val;
    },
    // 设置面包屑
    setCrumbsName: (state, val) => {
      state.crumbsName = val;
    },
    // 添加tab
    addTabList: (state, val) => {
      let homeIndex = state.tabList.findIndex((item) => {
        return item.path == contentPath + "/home";
      });
      // 不包含首页
      if (homeIndex == -1) {
        state.tabList.unshift({
          path: contentPath + "/home",
          name: "首页",
        });
      }
      let index = state.tabList.findIndex((item) => {
        return item.path == val.path;
      });
      // 选中的菜单不在打开的tab列表中
      if (index == -1) {
        state.tabList.push(val);
      }
      state.selectedTab = val;
    },
    // 清空table
    clearTab: (state, val) => {
      state.tabList = [];
      state.selectedTab = {};
    },
    // 删除tab
    removeTab: (state, path) => {
      state.tabList = state.tabList.filter((item) => {
        if (item.path == contentPath + "/home") {
          return true 
        } else {
          return item.path != path;
        }
      });
      // 如果删除的是当前选中的tab
      if (state.selectedTab.path == path) {
        state.selectedTab =
          state.tabList.length == 0
            ? {}
            : state.tabList[state.tabList.length - 1];
      }
    },
    // 清空tab
    cleanTab: (state) => {
      state.tabList = [];
      state.selectedTab = {};
    },
    // 设置菜单权限列表
    setMenuList: (state, val) => {
      state.menuList = val;
    },
    // 添加token到缓存中
    setToken: (state, val) => {
      state.token = val;
    },
    removeToken: (state) => {
      state.token = undefined;
    },
  },
});
